import * as TYPES from '../actions/actionTypes';
import o from '../components/o';
import Toast from 'react-native-root-toast'
import Constant from '../utils/Constant';
import { getToken } from './LoginAction';
export function loadMore(api, params) {
    return (dispatch, getState) => {
        const { dataList, hasMore, loading } = getState().Purchase;
        const { pageNum, keyword, pageSize, filter, expand, orderByDesc } = params;
        const skip = (pageNum - 1) * pageSize
        dispatch({ type: TYPES.PURCHASE_DATA_ING });
        const { token } = getState().User;
        const _api = api + "?token=" + token;
        if (!filter) {
            o(_api).take(pageSize).skip(skip).expand(expand).inlineCount().orderByDesc(orderByDesc).get().then(oHandler => {
                if(oHandler.data && oHandler.data[0].errmsg == Constant.ERRMSG){
                    // dispatch(getToken())
                }else{
                    dispatch(loadMoreSuccess(oHandler, params, dataList))
                }
            }).fail((e) => {
                dispatch(loadMoreFail())
            })
        }
    }
}

export function loadMoreSuccess(oHandler, params, dataList) {
    const { data, inlinecount } = oHandler;
    const { pageNum, pageSize, keyword } = params;
    let preData = dataList ? dataList : [];
    for (let i = 0; i < data.length; i++) {
        //计算单项plist中pnumber总和  进库的count 比较得出入库状态
        const purchase_plist = data[i].purchase_plist; //采购项
        let stocks = 0;//在库数
        const species = purchase_plist ? purchase_plist.length : 0;//商品种类
        let PNumber = 0 //总采购数
        for (let j = 0; j < purchase_plist.length; j++) {
            const PNumber_j = purchase_plist[j].PNumber;
            const into_j = purchase_plist[j].plist_into.length;
            PNumber = PNumber + PNumber_j;
            stocks = stocks + into_j;
        }
        const diff = PNumber - stocks;
        let state = 1;
        if (diff == 0) {
            state = 1 //已入库
        } else if (diff < PNumber && diff > 0) {
            state = 2//部分入库
        } else {
            state = 3//待入库
        }
        data[i].species = species;
        data[i].PNumber = PNumber;
        data[i].state = state;
        data[i].stocks = stocks;
        //图片处理
        const { erp_purchase_file } = data[i]
        if (erp_purchase_file) {
            let { Url } = erp_purchase_file;
            if (Url && Url.length > 0) {
                data[i].images = Url.split(',')
            }
        }
    }
    if (pageNum * pageSize < inlinecount) {
        return { type: TYPES.PURCHASE_DATA_SUCCESS, dataList: preData.concat(data), pageNum: pageNum, hasMore: true, keyword: keyword }
    } else {
        return { type: TYPES.PURCHASE_DATA_SUCCESS, dataList: preData.concat(data), pageNum: pageNum, hasMore: false, keyword: keyword }
    }
}
export function getDataFail(type) {
    Toast.show("获取数据异常", {
        duration: Toast.durations.LONG,
        position: Toast.positions.CENTER,
    })
    return { type: type }
}

export function initialDetail(item) {
    return (dispatch) => {
        dispatch({ type: TYPES.PURCHASE_DETAIL_DATA_INIT, data: item })
    }
}

export function getDetail(params) {
    return (dispatch, getState) => {
        dispatch({ type: TYPES.PURCHASE_DATA_DETAIL_ING });
        const { api, filter, select, expand } = params;
        const { token } = getState().User;
        const _api = api + "?token=" + token;
        o(_api).filter(filter).select(select).expand(expand).get().then(oHandler => {
            let purchasePlist = oHandler.data
            //计算每一个商品的待入库数  商品相关属性
            for (let i = 0; i < purchasePlist.length; i++) {
                let goods = purchasePlist[i].plist_archives;
                purchasePlist[i].Name = goods.Name;
                purchasePlist[i].Code = goods.Code;
                purchasePlist[i].stocks = purchasePlist[i].plist_into.length;
                const file = goods.archives_file;
                if (file) {
                    purchasePlist[i].images = file.Url.split(',')
                }
            }
            dispatch({ type: TYPES.PURCHASE_DATA_DETAIL_SUCCESS, data: purchasePlist })
        }).fail(e => {
            dispatch(getDataFail(TYPES.PURCHASE_DATA_DETAIL_FAIL))
        })
    }
}

//入库操作成功 返回list页面 更新list中入库状态
export function updatePurchaseList(params) {
    return (dispatch, getState) => {
        const { codesArr, PurchaseId } = params;
        let dataList_old = getState().Purchase.dataList;
        let dataList = [].concat(dataList_old)
        for (var i = 0; i < dataList.length; i++) {
            if (dataList[i].PurchaseId == PurchaseId) {
                const { PNumber, stocks } = dataList[i];
                const newstocks = stocks + codesArr.length;
                const diff = PNumber - newstocks;
                let state = 1;
                if (diff == 0) {
                    state = 1 //已入库
                } else if (diff < PNumber && diff > 0) {
                    state = 2//部分入库
                } else {
                    state = 3//待入库
                }
                dataList[i].stocks = newstocks;
                dataList[i].state = state;
            }
        }
        dispatch({ type: TYPES.PURCHASE_DATA_UPDATE, data: dataList })
    }
}

